package com.fingard.dsp.bank.directbank.czb01;

import java.util.Calendar;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.IBalance;
import com.fingard.dsp.bank.reqrespobj.balance.Req7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsBalDetail;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsHisBalDetail;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * Created by Fish on 2017/11/13.
 */
public class CZB01Balance extends CZB01Base implements IBalance {
    @Override
    public RetAts7506BalQry d_7506_BalQry(Req7506BalQry p_req7506) throws Exception {
        RetAts7506BalQry retAts = new RetAts7506BalQry(p_req7506.reqHeader);
        retAts.writeStart();
        try {
            CZB01Header bankHeader = new CZB01Header();
            bankHeader.transCode = "200108";
            bankHeader.reqSeqNo = p_req7506.reqHeader.transSeqID;

            XmlTextWriter xmlWriter = new XmlTextWriter();
            writeCommonHeader(xmlWriter, bankHeader);
            xmlWriter.writeStartElement("body");
            while (p_req7506.readRecord()) {
                xmlWriter.writeStartElement("record");
                xmlWriter.writeElementString("acno", p_req7506.readedDetail.actNum);
                xmlWriter.writeElementString("cur_code", convertCurToBank(p_req7506.readedDetail.actCur));
                xmlWriter.writeEndElement();
            }
            xmlWriter.writeEndElement();//body
            xmlWriter.writeEndElement();//ap
            xmlWriter.writeEndDocument();

            String[] retStrArr = sendToBank(xmlWriter.toXmlString());
            if (retStrArr[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(retStrArr[1], tmpSbMsg);

                if (tmpBkReader != null) {
                    String tmpRetCode = tmpBkReader.getFirstText("/ap/head/ret_code");
                    String tmpRetInfo = tmpBkReader.getFirstText("/ap/head/ret_info");
                    String tmpSuccFlag = tmpBkReader.getFirstText("/ap/head/succ_flag");
                    if ("0000".equals(tmpRetCode)) {
                        while (tmpBkReader.read("/ap/body/record")) {
                            RetAtsBalDetail tmpRetDetail = new RetAtsBalDetail();
                            tmpRetDetail.respCode = tmpSuccFlag;
                            tmpRetDetail.respInfo = tmpRetInfo;
                            tmpRetDetail.actNum = tmpBkReader.readedXElement.getSingleText("acno");
                            tmpRetDetail.actCur = convertCurToATS(tmpBkReader.readedXElement.getSingleText("cur_code"));
                            tmpRetDetail.currBal = tmpBkReader.readedXElement.getSingleText("balance");
                            tmpRetDetail.availBal = tmpBkReader.readedXElement.getSingleText("use_balance");
                            tmpRetDetail.frozenBal = tmpBkReader.readedXElement.getSingleText("freeze_bal");
                            retAts.writeRetDetail(tmpRetDetail, p_req7506.isBatch);
                        }
                    } else {
                        RetAtsBalDetail tmpRetDetail = new RetAtsBalDetail();
                        tmpRetDetail.respCode = "-1";
                        tmpRetDetail.respInfo = tmpRetInfo;
                        retAts.writeRetDetail(tmpRetDetail, p_req7506.isBatch);
                    }
                } else {
                    retAts.writeRespCode("-1");
                    retAts.writeRespInfo(tmpSbMsg.toString());
                }
            } else {
                retAts.writeRespCode("-1");
                retAts.writeRespInfo(retStrArr[1]);
            }
        } catch (Exception e) {
            retAts.writeRespCode("-1");
            retAts.writeRespInfo(e.getMessage());
            e.printStackTrace();
        }
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAts7505HisBalQry d_7505_BalQry(Req7505HisBalQry p_req7505) throws Exception {
        RetAts7505HisBalQry retAts = new RetAts7505HisBalQry(p_req7505.reqHeader);
        retAts.writeStart();
        try {
            CZB01Header bankHeader = new CZB01Header();
            bankHeader.transCode = "200107";
            bankHeader.reqSeqNo = p_req7505.reqHeader.transSeqID;

            XmlTextWriter xmlWriter = new XmlTextWriter();
            writeCommonHeader(xmlWriter, bankHeader);
            xmlWriter.writeStartElement("body");
            xmlWriter.writeStartElement("record");
            xmlWriter.writeElementString("acno", p_req7505.actNum);
            xmlWriter.writeElementString("cur_code", convertCurToBank(p_req7505.actCur));
            xmlWriter.writeEndElement();//record
            xmlWriter.writeEndElement();//body
            xmlWriter.writeEndElement();//ap
            xmlWriter.writeEndDocument();

            String[] retStrArr = sendToBank(xmlWriter.toXmlString());
            if (retStrArr[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(retStrArr[1], tmpSbMsg);

                if (tmpBkReader != null) {
                    RetAtsHisBalDetail tmpRetDetail = new RetAtsHisBalDetail();
                    String tmpRetCode = tmpBkReader.getFirstText("/ap/head/ret_code");
                    String tmpRetInfo = tmpBkReader.getFirstText("/ap/head/ret_info");
                    String tmpTrAcdt = tmpBkReader.getFirstText("/ap/head/tr_acdt");
                    if (tmpRetCode != null && tmpRetCode.equals("0000")) {
                        if (tmpBkReader.read("/ap/body/record")) {
                            tmpRetDetail.actBal = tmpBkReader.readedXElement.getSingleText("balance").replaceAll(",", "");
                            if (tmpTrAcdt.length() == 8) {
                                tmpRetDetail.balDate = tmpTrAcdt.substring(0, 4) + "-" + tmpTrAcdt.substring(4, 6) + "-" + tmpTrAcdt.substring(6, 8);
                            } else {
                                tmpRetDetail.balDate = tmpTrAcdt;
                            }
                            retAts.writeRetDetail(tmpRetDetail);
                        }
                    } else {
                        retAts.writeRespCode("-1");
                        retAts.writeRespInfo(tmpRetInfo);
                    }
                } else {
                    retAts.writeRespCode("-1");
                    retAts.writeRespInfo(tmpSbMsg.toString());
                }
            } else {
                retAts.writeRespCode("-1");
                retAts.writeRespInfo(retStrArr[1]);
            }
        } catch (Exception e) {
            retAts.writeRespCode("-1");
            retAts.writeRespInfo(e.getMessage());
            e.printStackTrace();
        }
        retAts.writeEnd();
        return retAts;
    }
}
